const Debounce = require("../template")

function testDebouncePerformance() {
  console.log(
    "1. 防抖函数性能测试 - 大量调用:",
    (() => {
      const start = performance.now()
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      for (let i = 0; i < 1000; i++) {
        debounced()
      }
      
      const end = performance.now()
      return (end - start) < 100 && callCount === 0 // 应该很快且不执行
    })()
  )

  console.log(
    "2. 防抖函数性能测试 - 内存使用:",
    (() => {
      const debounced = Debounce.debounce(() => {}, 100)
      const initialMemory = process.memoryUsage().heapUsed
      
      for (let i = 0; i < 10000; i++) {
        debounced()
      }
      
      const finalMemory = process.memoryUsage().heapUsed
      const memoryIncrease = finalMemory - initialMemory
      
      return memoryIncrease < 1024 * 1024 // 内存增长应该小于1MB
    })()
  )

  console.log(
    "3. 防抖函数性能测试 - 创建开销:",
    (() => {
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        Debounce.debounce(() => {}, 100)
      }
      
      const end = performance.now()
      return (end - start) < 50 // 创建1000个防抖函数应该很快
    })()
  )

  console.log(
    "4. 防抖函数性能测试 - 取消开销:",
    (() => {
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        const { debounced, cancel } = Debounce.debounceWithCancel(() => {}, 100)
        debounced()
        cancel()
      }
      
      const end = performance.now()
      return (end - start) < 50 // 1000次取消操作应该很快
    })()
  )

  console.log(
    "5. 防抖函数性能测试 - 刷新开销:",
    (() => {
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        const { debounced, flush } = Debounce.debounceWithFlush(() => {}, 100)
        debounced()
        flush()
      }
      
      const end = performance.now()
      return (end - start) < 50 // 1000次刷新操作应该很快
    })()
  )

  console.log(
    "6. 防抖函数性能测试 - 状态查询开销:",
    (() => {
      const { debounced, isPending, isCancelled } = Debounce.debounceWithState(() => {}, 100)
      
      const start = performance.now()
      
      for (let i = 0; i < 10000; i++) {
        isPending()
        isCancelled()
      }
      
      const end = performance.now()
      return (end - start) < 10 // 状态查询应该很快
    })()
  )

  console.log(
    "7. 防抖函数性能测试 - 条件执行开销:",
    (() => {
      const debounced = Debounce.debounceWithCondition(() => {}, 100, (args) => args[0] > 5)
      
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        debounced(i)
      }
      
      const end = performance.now()
      return (end - start) < 50 // 条件执行应该很快
    })()
  )

  console.log(
    "8. 防抖函数性能测试 - 优先级处理开销:",
    (() => {
      const highPriority = Debounce.debounceWithPriority(() => {}, 100, 10)
      const lowPriority = Debounce.debounceWithPriority(() => {}, 100, 1)
      
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        lowPriority()
        highPriority()
      }
      
      const end = performance.now()
      return (end - start) < 50 // 优先级处理应该很快
    })()
  )

  console.log(
    "9. 防抖函数性能测试 - 批量处理开销:",
    (() => {
      const debounced = Debounce.debounceWithBatch(() => {}, 100, 10)
      
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        debounced()
      }
      
      const end = performance.now()
      return (end - start) < 50 // 批量处理应该很快
    })()
  )

  console.log(
    "10. 防抖函数性能测试 - 错误处理开销:",
    (() => {
      const debounced = Debounce.debounceWithErrorHandling(() => {}, 100, () => {})
      
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        debounced()
      }
      
      const end = performance.now()
      return (end - start) < 50 // 错误处理应该很快
    })()
  )

  console.log(
    "11. 防抖函数性能测试 - 进度回调开销:",
    (() => {
      const debounced = Debounce.debounceWithProgress(() => {}, 100, () => {})
      
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        debounced()
      }
      
      const end = performance.now()
      return (end - start) < 50 // 进度回调应该很快
    })()
  )

  console.log(
    "12. 防抖函数性能测试 - 统计信息开销:",
    (() => {
      const { debounced, getStats } = Debounce.debounceWithStats(() => {}, 100)
      
      const start = performance.now()
      
      for (let i = 0; i < 1000; i++) {
        debounced()
        getStats()
      }
      
      const end = performance.now()
      return (end - start) < 50 // 统计信息应该很快
    })()
  )

  console.log(
    "13. 防抖函数性能测试 - 并发调用:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      const start = performance.now()
      
      // 模拟并发调用
      const promises = []
      for (let i = 0; i < 100; i++) {
        promises.push(Promise.resolve().then(() => debounced()))
      }
      
      return Promise.all(promises).then(() => {
        const end = performance.now()
        return (end - start) < 100 && callCount === 0
      })
    })()
  )

  console.log(
    "14. 防抖函数性能测试 - 长时间运行:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      const start = performance.now()
      
      // 长时间运行测试
      for (let i = 0; i < 10000; i++) {
        debounced()
        if (i % 1000 === 0) {
          // 每1000次检查一次性能
          const current = performance.now()
          if (current - start > 1000) break // 超过1秒就停止
        }
      }
      
      const end = performance.now()
      return (end - start) < 1000 && callCount === 0
    })()
  )

  console.log(
    "15. 防抖函数性能测试 - 内存泄漏检测:",
    (() => {
      const initialMemory = process.memoryUsage().heapUsed
      
      // 创建大量防抖函数
      const debouncedFunctions = []
      for (let i = 0; i < 1000; i++) {
        debouncedFunctions.push(Debounce.debounce(() => {}, 100))
      }
      
      // 调用它们
      for (let i = 0; i < 1000; i++) {
        debouncedFunctions[i]()
      }
      
      // 清理引用
      debouncedFunctions.length = 0
      
      // 强制垃圾回收
      if (global.gc) {
        global.gc()
      }
      
      const finalMemory = process.memoryUsage().heapUsed
      const memoryIncrease = finalMemory - initialMemory
      
      return memoryIncrease < 1024 * 1024 // 内存增长应该小于1MB
    })()
  )
}

testDebouncePerformance()
